package com.fire.social.controller;

import com.fire.social.config.Result;
import com.fire.social.config.Results;
import com.fire.social.mapper.PraiseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PraiseController {

    @Autowired
    private PraiseMapper praiseMapper;

    @PostMapping("/praised/zan")
    @ResponseBody
    public Result praiseShare(@RequestParam("account") String account, @RequestParam("contentId") Long contentId) {
        Integer number = praiseMapper.isPraised(contentId, account);
        if (number > 0) {
            Long praiseNumber = praiseMapper.getPraisedNumber(contentId);
            return Results.success(praiseNumber);
        }
        Long relationshipId = praiseMapper.praisedIt(account, contentId);
        Long praiseNumber = praiseMapper.getPraisedNumber(contentId);
        if (relationshipId != null) {
            return Results.success(praiseNumber);
        }
        return Results.failure();
    }

    @PostMapping("/praised/unzan")
    @ResponseBody
    public Result unPraiseShare(@RequestParam("account") String account, @RequestParam("contentId") Long contentId) {
        Integer affectRowes = praiseMapper.canclepraised(account, contentId);
        Long praiseNumber = praiseMapper.getPraisedNumber(contentId);
        if (affectRowes > 0) {
            return Results.success(praiseNumber);
        }
        return Results.failure();
    }

}
